-
Notifications
You must be signed in to change notification settings - Fork 3
tutorial/handling null values in protobuf and avro #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LG so far @Cerchie!
.setTotalCost(random.nextDouble() * random.nextInt(100)); | ||
``` | ||
|
||
In this case, you'll receive a NullPointer error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whats going to come next? the field being oneof
where one is a boolean to signify null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has to be a protobuf schema addition... if I were to show it I think it'd be a oneof, but also there's a google wrapper option. unfortunately because of the way the API is defined with protocol buffers and kafka there are two methods with the same name and java chokes on the protocol buffer... not sure how to fix here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does optional
do the trick? i.e. does everything just work if you do that? I'm looking at the guidance here:
If not using optional would add complexity or ambiguity, then use optional primitive fields. Wrapper types must not be used going forward.
which makes me think we might want to avoid the wrapper pattern... I'm not sure why this is the guidance, e.g., will wrapper types eventually be deprecated?
I'm thinking that if optional
works, go with that, and we'd also want to update this docs section to recommend optional
rather than use a wrapper
wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so with this protobuf definition:
message Purchase {
optional string item = 1;
double total_cost = 2;
string customer_id = 3;
}
you still end up with a NullPointerException
¯_(ツ)_/¯
but that's a good point about the guidance, I'll pass it on to docs.
Co-authored-by: Dave Troiano <[email protected]>
handling-null-values-in-avro-and-protobuf/kafka/src/main/proto/purchase.proto
Show resolved
Hide resolved
# How to allow `null` field values in Avro and Protobuf | ||
Let's say you're using an Avro or Protobuf schema, and sometimes you want to set a field named `item` to null. Say it's a pipeline that takes both donations and purchases to be filtered later, and the donations are processed as purchases with null items. How to adjust the schema to allow for a null value? | ||
|
||
Avro natively supports null fields with the 'null' type. In the above example, in order to make the `item` field nullable, you can allow the type to be "string" or "null" in the following manner: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avro natively supports null fields with the 'null' type. In the above example, in order to make the `item` field nullable, you can allow the type to be "string" or "null" in the following manner: | |
Avro natively supports null fields with the `null` type. In the above example, in order to make the `item` field nullable, you can allow the type to be "string" or "null" in the following manner: |
From the top-level directory: | ||
|
||
``` | ||
./gradlew clean :handling-null-values-in-avro-and-protobuf:kafka:test --info |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this didn't work for me on a fresh clone:
* What went wrong:
A problem occurred evaluating project ':handling-null-values-in-avro-and-protobuf:kafka'.
> /Users/dtroiano/Desktop/tutorials/handling-null-values-in-avro-and-protobuf/kafka/src/main/resources/confluent.properties (No such file or directory)
.setTotalCost(random.nextDouble() * random.nextInt(100)); | ||
``` | ||
|
||
In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/). | |
In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/). |
.setTotalCost(random.nextDouble() * random.nextInt(100)); | ||
``` | ||
|
||
In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you planning to have an example with wrapper? or leave it as an exercise to the reader
.setTotalCost(random.nextDouble() * random.nextInt(100)); | ||
``` | ||
|
||
In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest adding a clean up step that directs the user to delete the env created for this
need to figure out a smooth way to handle testing and a way to teach the reader about what happens when null values are handled/not. tagging @davetroiano